package com.example.administrator.himalaya.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.administrator.himalaya.activity.DetailsActivity;
import com.example.administrator.himalaya.entity.categoryentity.DataEntity;

import java.util.List;

/**
 * Created by Administrator on 2016/12/4.
 */

public class DataAdapter extends PagerAdapter {
    private List<ImageView> imagelist;
    private Context context;
    private List<DataEntity>  datalist;
    private String link;

    public DataAdapter(List<ImageView> imagelist, Context context, List<DataEntity> datalist) {
        this.imagelist = imagelist;
        this.context = context;
        this.datalist = datalist;
    }

    @Override
    public int getCount() {
        return imagelist.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        imagelist.get(position).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent =new Intent(context, DetailsActivity.class);
                switch (position){
                    case 0:
                        link = datalist.get(0).getLink();
                        intent.putExtra("link",link);
                        context.startActivity(intent);
                        break;
                    case 1:
                        link = datalist.get(1).getLink();
                        intent.putExtra("link",link);
                        context.startActivity(intent);
                        break;
                    case 2:
                        link = datalist.get(2).getLink();
                        intent.putExtra("link",link);
                        context.startActivity(intent);
                        break;
                }
            }
        });
        container.addView(imagelist.get(position));
        return imagelist.get(position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
//        super.destroyItem(container, position, object);
        container.removeView(imagelist.get(position));
    }
}
